package com.gujiangbo.multithreadskill;

import java.util.concurrent.TimeUnit;

/**
 * yield() 放弃当前CPU的资源,将它让给其他的任务去占用CPU执行时间，但放弃的时间不确定，有可能刚刚放弃，马上又获得CPU的时间片
 *
 * @author gujiangbo
 * @date 2020/10/24
 */
public class Yield {

    public static void main(String[] args) {
        YieldThread t1 = new YieldThread();
        t1.start();
    }

}

class YieldThread extends Thread {


    @Override
    public void run() {
        long beginTime = System.currentTimeMillis();
        int count = 0;
        for (int i = 0; i < 30000000; i++) {
            // Thread.yield();
            count = count + (i + 1);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("耗时:" + (endTime - beginTime) + "毫秒~");
    }
}
